package com.cskaoyan.shopping.service.impl;


import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;

import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.weaver.ast.And;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

@Service
public class IProductServiceImpl implements IProductService {


    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ProductConverter productConverter;
    @Autowired
    ContentConverter contentConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());

            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());

            ProductDetailDto productDetailDto = new ProductDetailDto();

            productDetailDto.setProductId(item.getId());
            productDetailDto.setProductName(item.getTitle());
            productDetailDto.setSalePrice(item.getPrice());
            productDetailDto.setSubTitle(item.getSellPoint());
            productDetailDto.setProductImageBig(item.getImageBig());
            productDetailDto.setProductImageSmall(Arrays.asList(item.getImages()));
            productDetailDto.setDetail(itemDesc.getItemDesc());


            // 业务执行成功
            response.setProductDetailDto(productDetailDto);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());


        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());

        }
        return response;


    }

    @Override
    public AllProductResponse getAllProduct(@RequestBody AllProductRequest request) {
        AllProductResponse response = new AllProductResponse();
        try {
            request.requestCheck();

            PageHelper.startPage(request.getPage(), request.getSize());
            Example example = new Example(Item.class);
            Example.Criteria criteria = example.createCriteria();
            if (request.getCid() != null) {
                criteria.andEqualTo("cid", request.getCid());
            }
            if ("1" .equals(request.getSort())) {
                example.setOrderByClause("price ASC");
            }
            if ("-1" .equals(request.getSort())) {
                example.setOrderByClause("price DESC");
            }
            if (!"".equals(request.getPriceGt())&&"".equals(request.getPriceLte())) {
                criteria.andGreaterThanOrEqualTo("price", request.getPriceGt());
            }
            if (!"".equals(request.getPriceLte()) && "".equals(request.getPriceGt())) {
                criteria.andLessThanOrEqualTo("price", request.getPriceLte());
            }
            if ("".equals(request.getPriceGt()) && "".equals(request.getPriceLte())) {
                criteria.andBetween("price", request.getPriceGt(), request.getPriceLte());
            }

            List<Item> items = itemMapper.selectByExample(example);
            PageInfo<Item> itemPageInfo = new PageInfo<>(items);

            List<ProductDto> productDtos = productConverter.items2Dto(items);
            // 业务执行成功

            response.setData(productDtos);
            response.setTotal(itemPageInfo.getTotal());
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }

        return response;
    }

    @Override
    public RecommendResponse getRecommendGoods() {

        RecommendResponse response = new RecommendResponse();
        try {
            Example example = new Example(Panel.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("name","热门推荐");
            List<Panel> panels = panelMapper.selectByExample(example);

            HashSet<PanelDto> panelDtos = new HashSet<>();
            for (Panel panel : panels) {
                PanelDto panelDto = contentConverter.panel2Dto(panel);
                List<PanelContentItem> panelContentItems
                        = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                List<PanelContentItemDto> panelContentItemDtos
                        = contentConverter.panelContentItem2Dto(panelContentItems);
                   panelDto.setPanelContentItems(panelContentItemDtos);
                   panelDtos.add(panelDto);
            }
            // 业务执行成功
            response.setPanelContentItemDtos(panelDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }

        return response;

    }

    @Override
    public AllItemResponse getAllItems() {
        return null;
    }
}
